package com.blockchain.controller;


import com.blockchain.service.UserService;
import com.blockchain.service.dto.RegistrationRequestDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RegisterController {
    private final UserService userService;
    @Autowired
    public RegisterController(UserService userService){
        this.userService=userService;
    }
    @PostMapping("/register")
    public ResponseEntity<String> registerUser(@RequestBody RegistrationRequestDTO registrationRequestDTO){
        try {
            //检查重复
            boolean isUsernameDuplicate=userService.checkUsernameDuplicateUser(registrationRequestDTO);
            if(isUsernameDuplicate){
                //返回错误信息
                return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                        .body("该用户名已被注册");
            }
            boolean isEmailDuplicate=userService.checkEmailDuplicateUser(registrationRequestDTO);
            if(isEmailDuplicate){
                //返回错误信息
                return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                        .body("该邮箱已被注册");
            }
            userService.registerUser(registrationRequestDTO);
            return ResponseEntity.ok("User registered successfully");
        }catch (Exception e)
        {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("Fail to register User"+e.getMessage());
        }

    }
}
